%%%-------------------------------------------------------------------
%%% @author liuwentao
%%% @doc
%%%     进程管理
%%% @end
%%% Created : 08. 7月 2021 17:58
%%%-------------------------------------------------------------------
-module(services).
-author("liuwentao").
-export([
    config/1
    ,get/1
]
).

-include("service.hrl").

%% shell节点
-spec cfg(atom()) -> {ok, list()} | {error, undefined}.
cfg(shell) ->
    {ok, []};

%% 普通节点
cfg(node) ->
    {ok, [log, info, cross]};

%% 中央服
cfg(center) ->
    {ok, []};
%% 云服务器
cfg(cloud) ->
    {ok, [
    ]};

%% 游戏区
%% 所有启动的逻辑服务需要在 tcp_sup, tcp_listener之前
cfg(zone) ->
    {ok, [
        %% ===公共模块(前置)===
        role_data, role_query, role_group
        %% 系统相关模块放最后面,顺序不能随便改变
        , tcp_sup, tcp_listener
    ]
    };

cfg(_) ->
    {error, undefined}.

%% @doc 获取指定服务的配置数据
-spec get(atom()) -> {ok, #service{}} | {error, undefined}.
get(tcp_sup = Id) ->
    {ok, #service{
        id = Id
        ,name = "acceptor监控树"
        ,mfa = {tcp_sup, start_link, []}
        ,type = supervisor
    }
    };
get(tcp_listener = Id) ->
    {ok, #service{
        id = Id
        ,name = "socket监听服务"
        ,mfa = {tcp_listener, start_link, []}
        ,depend_on = [env, sup_acceptor]
    }
    };
get(role_data = Id) ->
    {ok, #service{
        id = Id
        ,name = "角色数据存取服务"
        ,mfa = {role_data, start_link, []}
    }
    };
get(role_group = Id) ->
    {ok, #service{
        id = Id
        ,name = "角色分组管理器"
        ,mfa = {role_group, start_link, []}
    }
    };
get(role_query = Id) ->
    {ok, #service{
        id = Id
        ,name = "角色查询服务"
        ,mfa = {role_query, start_link, []}
    }
    };
get(_Name) ->
    {error, undefined}.

%% @doc 读取默认配置
-spec config(atom()) -> {ok, list()} | {error, undefined}.
config(Type) ->
    case cfg(Type) of
        {ok, L} -> {ok, parse(L, Type, [])};
        Else -> Else
    end.

%% 统一处理成带参数的格式
parse([], _Type, L) -> lists:reverse(L);
parse([Id = role_group | T], Type, L) -> parse(T, Type, [{Id, [Type]} | L]);
parse([Id | T], Type, L) -> parse(T, Type, [{Id, []} | L]).
